* =============================================================================
* File Name: 2016_EC_EnvRegulation--CreateFullNPRI_ASM_March2019.do

* File Description: This file creates the micro data. It opens the full NPRI and 
* trims it, then merges in the ASM-NPRI key, then merges the NPRI with the ASM. 


* The merge is performed on plant and year. 

* The files main output is the dta file "npri_asm_2002to2012.dta".

* Creation date: March 6, 2019

* This version: March 6, 2019

* Author: Nouri Najjar
* =============================================================================

* Loop over each pollutant
foreach x in PM25 {


* -----------------------------------------------------------------------------
* Section 1: Call the npri data ("npri_final.dta") from the cder 
* server. 

* -----------------------------------------------------------------------------
* Set server folder. 
local datadir \\f4cder01\2016_EC_EnvRegulation\DATA\

* Open dataset.
use "`datadir'npri_final.dta", clear
* -----------------------------------------------------------------------------


* -----------------------------------------------------------------------------
* Section 2: Prepare the npri data for merging
* -----------------------------------------------------------------------------
*******
* Restrict Sample to Pollutant of Interest
*******
gen PM25=(cas_number=="NA - M10")
gen NOx=(cas_number=="11104-93-1")

keep if `x'==1
*******	 

* create numeric year variable
destring rep_period, gen(yr4)
* Only keep 2002 to 2012
keep if yr4>=2002 & yr4<=2012
* Drop any duplciats
bysort npri_id rep_period: gen dupID = cond(_N==1,0,_n)
drop if dupID>1
* Keep variables
keep cas_number rep_period npri_id cmauid naics6_npri total_all_ units
* Sort on plant ID and year
sort npri_id rep_period
* Save for merge
save "`datadir'npri_final_merge`x'.dta", replace
* -----------------------------------------------------------------------------

* -----------------------------------------------------------------------------
* Section 3: prepare key dataset
* -----------------------------------------------------------------------------
* Open key data
use "`datadir'npri_asm_key.dta", clear
* Drop non-npri plants
keep if missing(npri_id)==0
* Drop any duplciats
bysort npri_id rep_period: gen dupID = cond(_N==1,0,_n)
drop if dupID>1
drop dupID
* Save
save "`datadir'npri_asm_key_merge`x'.dta", replace
* -----------------------------------------------------------------------------

* -----------------------------------------------------------------------------
* Section 4: merge ASM key 
* -----------------------------------------------------------------------------
* Open data
use "`datadir'npri_final_merge`x'.dta", clear
* Merge
merge 1:1 npri_id rep_period using "`datadir'npri_asm_key_merge`x'.dta"
keep if _merge==3
* Drop duplicates
bysort s_meadest yr4: gen dupID = cond(_N==1,0,_n)
drop if dupID>1
* Keep variables, including ASM ID (s_meadest) and yr4
keep cas_number rep_period npri_id cmauid naics6_npri total_all_ units s_meadest yr4
* Save
save "`datadir'npri_final_merge_key`x'.dta", replace
* -----------------------------------------------------------------------------

* -----------------------------------------------------------------------------
* Section 5: merge full npri to full ASM
* -----------------------------------------------------------------------------
* Open full asm
use "`datadir'asm2012.dta", clear

* Sort on meadest and year
sort s_meadest yr4
* Merge in NPRI data
merge m:1 s_meadest yr4 using "`datadir'npri_final_merge_key`x'.dta"
* Save
save "`datadir'asm_npri_2002to2012`x'.dta", replace
* -----------------------------------------------------------------------------

}
